/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controlers;

import Pojos.ThanhVien;
import Pojos.NhomThanhVien;
import Util.MyUtil;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 *
 * @author ToanNhan
 */
@WebServlet(name = "DangKyThanhVien", urlPatterns = {"/DangKyThanhVien"})
public class DangKyThanhVien extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String command = request.getParameter("command");
            if (command != null && command.toLowerCase().compareTo("dangkythanhvien") == 0) {
                String userName = request.getParameter("userName");
                String password = request.getParameter("password");
                password = MyUtil.maHoaMD5(password);
                String email = request.getParameter("email");
                String fullName = request.getParameter("fullName");
                String temp = request.getParameter("dateOfBirth");
                DateFormat df = new SimpleDateFormat("dd-mm-yyyy");
                //temp = "26/12/1989";
                Date dateOfBirth = new Date(System.currentTimeMillis());
                try
                {
                    dateOfBirth = new Date(df.parse(temp).getTime());
                }
                catch (Exception ex){
                    System.out.println(ex.getMessage());
                }

                String address = request.getParameter("address");
                String phoneNumber = request.getParameter("phoneNumber");
                boolean gioiTinh = Boolean.valueOf(request.getParameter("gioiTinh"));
                SessionFactory factory = new Configuration().configure("Pojos/hibernate.cfg.xml").buildSessionFactory();
                Session session = factory.openSession();

                ThanhVien thanhVien = new ThanhVien(userName, password, new NhomThanhVien("NTV1", ""), fullName, dateOfBirth, address, email, phoneNumber, gioiTinh,
                        new Date(System.currentTimeMillis()), 1);
                
                session.beginTransaction();
                session.save(thanhVien);
                session.getTransaction().commit();
                session.close();
                factory.close();
                response.sendRedirect("/WebsiteBanHang/DangKyThanhCong.jsp");
            } else{
                response.sendRedirect("/WebsiteBanHang/DangKyThanhVien.jsp");
            }
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
